package com.jmgd.cjlx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    //时间戳转换日期方法
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){ format = "yyyy-MM-dd HH:mm:ss";}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

    public static Date getMonthFirstDay(int amount) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

//获取前月的第一天

        Calendar cal_1=Calendar.getInstance();//获取当前日期

        cal_1.add(Calendar.MONTH, amount);

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

        String firstDay = format.format(cal_1.getTime());

        return format.parse(firstDay);

    }

    public static Date getMonthLastDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


//获取前月的最后一天

        Calendar cale = Calendar.getInstance();

        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天

        String lastDay = format.format(cale.getTime());

        return cale.getTime();
    }

    public static String getMonthYYYYMM(int amount){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");

//获取前月的第一天

        Calendar cal_1=Calendar.getInstance();//获取当前日期

        cal_1.add(Calendar.MONTH, amount);

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

        String firstDay = format.format(cal_1.getTime());

        return firstDay;

    }



    public static void main(String[] args) throws ParseException {
        System.out.println(DateUtil.getMonthFirstDay(0));
        DateUtil.getMonthLastDay();
        System.out.println(DateUtil.getMonthYYYYMM(-2));
    }
}
